<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Map;

use InvalidArgumentException;
use Traversable;
use Skyline\Comparator\ComparatorInterface;
use Skyline\Predicate\PredicateInterface;
use Skyline\Structure\AbstractContainer;
use Skyline\Structure\Set\HashSet;

/**
 * Abstract implementation of MapInterface
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractMap extends AbstractContainer implements MapInterface
{
	/**
	 * @var ComparatorInterface
	 */
	protected $comparator;
	
	/**
	 * @var PredicateInterface
	 */
	protected $keyPredicate;
	
	/**
	 * @var PredicateInterface
	 */
	protected $valuePredicate;
	
	/**
	 * Associates multiple keys with multiple values in the map
	 * 
	 * @param Traversable $entries
	 * @return void
	 */
	public function setAll($entries)
	{
		if(!is_array($entries) && !($entries instanceof Traversable)) {
			throw new InvalidArgumentException('Expected traversable object or array');
		}
		foreach($entries as $key => $value) {
			$this->set($key, $value);
		}
	}
	
	/**
	 * Sets the comparator used to sort this map
	 * 
	 * @param ComparatorInterface $comparator
	 * @return void
	 */
	public function setComparator(ComparatorInterface $comparator)
	{
		$this->comparator = $comparator;
	}
	
	/**
	 * Sets the predicate used to validate map keys
	 * 
	 * @param PredicateInterface $keyPredicate
	 * @return void
	 */
	public function setKeyPredicate(PredicateInterface $keyPredicate)
	{
		$this->keyPredicate = $keyPredicate;
	}
	
	/**
	 * Sets the predicate used to validate map values
	 * 
	 * @param PredicateInterface $valuePredicate
	 * @return void
	 */
	public function setValuePredicate(PredicateInterface $valuePredicate)
	{
		$this->valuePredicate = $valuePredicate;
	}
	
	/**
	 * @throws InvalidArgumentException If predicate evaluates key to false
	 * @throws InvalidArgumentException If predicate evaluates value to false
	 * @param mixed $key
	 * @param mixed $value
	 * @return void
	 */
	protected function assertEntryIsValid($key, $value)
	{
		if(!is_null($this->keyPredicate) && !$this->keyPredicate->evaluate($key)) {
			throw new InvalidArgumentException('Key not valid, per predicate');
		}
		if(!is_null($this->valuePredicate) && !$this->valuePredicate->evaluate($value)) {
			throw new InvalidArgumentException('Value not valid, per predicate');
		}
	}
	
	/**
	 * Creates a new set instance used to create different views of the map
	 * 
	 * @param Traversable $elements [optional]
	 * @return SetInterface
	 */
	protected function createSet($elements = null)
	{
		return new HashSet($elements);
	}
}